import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

// 引入动态路由
// 员工
import employeesRouter from '@/router/modules/employees'
// 审批
import approvalsRouter from '@/router/modules/approvals'
// 考勤
import attendancesRouter from '@/router/modules/attendances'
// 组织架构
import departmentsRouter from '@/router/modules/departments'
// 权限管理
import permissionRouter from '@/router/modules/permission'
// 工资
import salarysRouter from '@/router/modules/salarys'
// 角色管理
import settingRouter from '@/router/modules/setting'
// 社保
import socialRouter from '@/router/modules/social'
// 动态路由列表
export const asyncRouter = [
  departmentsRouter,
  settingRouter,
  employeesRouter,
  permissionRouter,
  approvalsRouter,
  attendancesRouter,
  salarysRouter,
  socialRouter
]
// 静态路由
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true // hidden 为 true不会出现在侧边导航
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  // 首页管理
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: { title: '首页', icon: 'dashboard' }
      }
    ]
  },
  // Excel 导入导出
  {
    path: '/import',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '',
        name: 'import',
        component: () => import('@/views/Import/index')
      }
    ]
  },
  // 404 page must be placed at the end !!!
  { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }), // 如果滚动到底部，切换会回到顶部
  // 合并静态路由和动态路由
  routes: [...constantRoutes, ...asyncRouter]
})

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
